<?php
//send mobile code
function send_mobile_code($mobile,$type){
	header("Content-Type:text/html;charset=UTF-8");
	$ret['state'] = check_code_time($mobile, $type);
//	$ret['state'] = true;
	if ($ret['state']) {
		//type 0 注册
		$data = array('mobile' => $mobile, 'code' => rand(1000, 9999), 'type' => $type, 'send_time' => time());
		$paramArr = array(
			'method' => 'alibaba.aliqin.fc.sms.num.send',
			'app_key' => '23300165',
			'timestamp' => date('Y-m-d H:i:s'),
			'format' => 'json',
			'v' => '2.0',
			'sign_method' => 'md5',
			'sms_type' => 'normal',
			'sms_free_sign_name' => '注册验证',
			'sms_param' => '{"code":"' . $data['code'] . '","product":"西安宝玉林棋牌运动俱乐部"}',
			'rec_num' => $mobile,
			'sms_template_code' => 'SMS_4730048',
		);
		$sign = createSign($paramArr);
		$strParam = createStrParam($paramArr);
		$strParam .= 'sign=' . $sign;
		$url = 'http://gw.api.taobao.com/router/rest?' . $strParam;
		mobile_code($data);
		$result = json_decode(file_get_contents($url), true);
		$ret['msg'] = '发送成功';
		$ret['status'] = 1;
		return $ret;
	}else{
		return array('status' => 0, 'msg' => '亲,等一会再发送吧!');
	}
}

//验证码相关
function get_mobile_code($mobile, $type){
	$CI = &get_instance();
	$CI->load->database();
	return db_get_obj('mobile_code',array('mobile' => $mobile, 'type' => $type));
}
function check_mobile_code($mobile, $type, $code){
	$ret = false;
//	if($code == '02102205'){
//		return true;
//	}else{
		$CI = &get_instance();
		$CI->load->database();
		$mobile_code = db_get_obj('mobile_code',array('mobile' => $mobile, 'type' => $type, 'send_time > ' => (time() - 1800)));
		if($mobile_code['code'] == $code){
			$ret = true;
		}else{
			$ret = false;
		}
		return $ret;
//	}
}
function mobile_code($data){
	$CI = &get_instance();
	$CI->load->database();
	$code = db_get_obj('mobile_code',array('mobile' => $data['mobile'], 'type' => $data['type']));
	if($code){
		$CI->db->update('mobile_code', $data, array('id' => $code['id']));
	}else{
		$data['ip'] = @get_client_ip();
		$data['ctime'] = time();
		$CI->db->insert('mobile_code', $data);
	}
}
function check_code_ip(){
	$ret = true;
	$CI = &get_instance();
	$CI->load->database();
	$code = db_get_list('id', 'mobile_code', "ip='@get_ip()' and ctime > ".(time()-86400) );
	if( count($code) >= 5 ){
		$ret = false;
	}
	return $ret;
}
function check_code_time($mobile, $type){
	$ret = true;
	$CI = &get_instance();
	$CI->load->database();
	$code = db_get_obj('mobile_code',array('mobile' => $mobile, 'type' => $type));
	if ($code){
		if (($code['send_time'] + 120) > time()){
			$ret = false;
		}
	}
	return $ret;
}

function createSign ($paramArr) {
	$appSecret = 'f723e2ecc63db9cfa1b4be0e49caa469';
	$sign = $appSecret;
	ksort($paramArr);
	foreach ($paramArr as $key => $val) {
		if ($key != '' && $val != '') {
			$sign .= $key.$val;
		}
	}
	$sign.=$appSecret;
	$sign = strtoupper(md5($sign));
	return $sign;
}
//组参函数
function createStrParam ($paramArr) {
	$strParam = '';
	foreach ($paramArr as $key => $val) {
		if ($key != '' && $val != '') {
			$strParam .= $key.'='.urlencode($val).'&';
		}
	}
	return $strParam;
}

function is_phone($phone) {
	if (!$phone)return false;
	$phone = trim($phone);
	if (strlen($phone) != 11)return false;
//	preg_match('/1[34578]{1}\d{9}$/',$mobile)       //以后用这个验证
	if(!preg_match("/^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/",$phone)){
		return false;
	}
	return true;
}